package com.ibbt.musicplayer.client.panels;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.gwittit.client.facebook.ConnectState;
import com.ibbt.musicplayer.client.service_interfaces.RadioStationService;
import com.ibbt.musicplayer.client.service_interfaces.RadioStationServiceAsync;
import com.ibbt.musicplayer.model.Radio;

public class OverviewStationsPanel extends FlowPanel {

	private List<Radio> stations;
	private RadioStationServiceAsync radiostation_service;
	private Grid grid;

	public OverviewStationsPanel() {

		// retrieve the stations from the database via RPC-service
		//getStationsFromServer();
	}

	private void getStationsFromServer() {
		// Service initialiseren die de radiozenders ophaalt
		radiostation_service = (RadioStationServiceAsync) GWT.create(RadioStationService.class);

		// Radiozenders ophalen
		radiostation_service.getRadioStations(new getRadioStationsCallback());
	}

	// Construct the layout of the panel
	private void buildLayout(List<Radio> result) {
		int cols = 4;
		int rows = result.size() / cols;

		grid = new Grid(cols, rows);

		int teller = 0;
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				Radio station = result.get(teller);
				teller++;
				grid.setText(i, j, station.getName()); // later door image vervangen
			}
		}
		
		this.add(grid);

	}
	
	/*************************** CALLBACK CLASSES *********************/
	
	/** Callback fired when retrieving radiostations */
	private class getRadioStationsCallback implements AsyncCallback<List<Radio>> {
		public void onFailure(Throwable caught) {
			Window.alert("failed to get stations");
		}

		public void onSuccess(List<Radio> result) {
			// build the layout
			buildLayout(result);
		}
	};
}
